extends Node2D

var strength:float=0

@onready var camera_2d: Camera2D = $Camera2D
var block=preload("res://scenes/block.tscn")

func _ready() -> void:
	var generator=Maze.new()
	var maze = generator.generate_maze_dfs(randi_range(4,9),randi_range(4,9))
	for i in range(0,len(maze)):
		for a in range(0,len(maze[0])):
			var blkinfo=maze[i][a]
			var blk=block.instantiate() as Node2D
			blk.position.x=a*32.0
			blk.position.y=i*32.0
			if not blkinfo[0]:
				blk.get_node("StaticBody2D/top").hide()
				blk.get_node("StaticBody2D/top").disabled=true
			if not blkinfo[1]:
				blk.get_node("StaticBody2D/bottom").hide()
				blk.get_node("StaticBody2D/bottom").disabled=true
			if not blkinfo[2]:
				blk.get_node("StaticBody2D/left").hide()
				blk.get_node("StaticBody2D/left").disabled=true
			if not blkinfo[3]:
				blk.get_node("StaticBody2D/right").hide()
				blk.get_node("StaticBody2D/right").disabled=true
			add_child(blk)
	var viewport_size = get_viewport_rect().size  # 获取视口大小
	
	var map_size=Vector2(len(maze[0]),len(maze))*32.0
	var zoom_x=viewport_size.x/map_size.x
	var zoom_y=viewport_size.y/map_size.y
	var min_zoom=min(zoom_x,zoom_y)
	camera_2d.zoom=Vector2(min_zoom,min_zoom)
	camera_2d.position=map_size/2-Vector2(16,16)
	$tank1.position.x=randi_range(0,maze[0].size()-1)*32
	$tank1.position.y=randi_range(0,maze.size()-1)*32
	$tank2.position.x=randi_range(0,maze[0].size()-1)*32
	$tank2.position.y=randi_range(0,maze.size()-1)*32

func shake_camera(st:float):
	strength=st

func _process(delta: float) -> void:
	if not is_zero_approx(strength):
		$Camera2D.offset.x=randf_range(-strength,strength)
		$Camera2D.offset.y=randf_range(-strength,strength)
		strength = move_toward(strength, 0, 16*delta)
	else:
		strength=0

		
